package leetcode.leetcode217;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author czt
 * @version 1.0
 * @since 2025/1/7
 */
public class Leetcode217 {

    public boolean containsDuplicate(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                return true;
            } else {
                map.put(nums[i], i);
            }
        }
        return false;
    }

    public boolean containsDuplicate1(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            // 如果可以添加成功，返回true；有重复元素，返回false
            if (!set.add(num)) {
                return true;
            }
        }
        return false;
    }
}
